package com.sofwin.controller;

import com.sofwin.Constants;
import com.sofwin.pojo.ListUtil;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @author: andyliu
 * @date: 2022/8/11 15:57
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public String login(SysUser user, String code, HttpSession session, Model model){
        if(StringUtils.isEmpty(code)){
            return "redirect:index.jsp";
        }
        Object sessionCode = session.getAttribute("current");
        if(sessionCode==null){
            return "redirect:index.jsp";
        }
        if(code.toUpperCase().equals(sessionCode.toString().toUpperCase())) {
            // 操作数据  1
            SysUser loginUser = userService.checkLogin(user);
            if (loginUser!=null) {
                // 登录成功
                // 2
                // 用户信息放入session
                List<ListUtil> menus = userService.queryListUtil(loginUser.getId());
                model.addAttribute("menus",menus);
                session.setAttribute(Constants.CURRENT_USER,loginUser);
                return "index";
            }

        }
        return "redirect:index.jsp";
    }
}
